package com.taotao.common.utils;

import java.io.File;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;


/**
 * 腾讯云cos服务器上传工具类
 * @author Mr.song
 * @date 2019/06/08 20:52
 */
public class TencenUploadUtil {

    //腾讯云的SecretId
	public static String secretId="AKIDRffOq2w2e2B6lKvnhLjrkNkroX3Jr6cw";
    //腾讯云的SecretKey
    public static String secretKey="EH6gz3sRknfJhphNoztc6KI0TiiT3OTH";
    //腾讯云的bucket (存储桶)
    public static String bucket="easygo-1256152350";
    //腾讯云的region(bucket所在地区)
    public static String region="ap-shanghai";
    //腾讯云的访问基础链接:
    public static String baseUrl="https://easygo-1256152350.cos.ap-shanghai.myqcloud.com/";
    
    public static void init(String secretId,String secretKey,String bucket,String region,String baseUrl){
    	TencenUploadUtil.secretId=secretId;
    	TencenUploadUtil.secretKey=secretKey;
    	TencenUploadUtil.bucket=bucket;
    	TencenUploadUtil.region=region;
    	TencenUploadUtil.baseUrl=baseUrl;
    }

    
    /**
     * 上传文件
     *
     * @param path 文件服务器下的根路径,即key,如: doc/picture.jpg
     * @param file
     * @return 成功返回文件路径,失败返回null
     */
    public static String uploadFile(String path, File file) {
    	COSClient cosclient = null;
    	String rtValue = null;
    	try {
	        // 1 初始化用户身份信息(secretId, secretKey)
	        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
	        ClientConfig clientConfig = new ClientConfig(new Region(region));
	        cosclient = new COSClient(cred, clientConfig);
	        String bucketName = bucket;
	        // 上传 object, 建议 20M 以下的文件使用该接口
	        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, path, file);
	        ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setCacheControl("max-age=172800");//设置缓存时间48小时
	        putObjectRequest.setMetadata(objectMetadata);
	      
            PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
            String etag = putObjectResult.getETag();
            rtValue = baseUrl + path;
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cosclient.shutdown();
        }
		return rtValue;
    }
    

	public static void deleteObject(String imageUrl) {
		COSClient cosclient = null;
    	String rtValue = null;
    	try {
	        // 1 初始化用户身份信息(secretId, secretKey)
	        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
	        ClientConfig clientConfig = new ClientConfig(new Region(region));
	        cosclient = new COSClient(cred, clientConfig);
	        String bucketName = bucket;
            cosclient.deleteObject(bucketName, imageUrl);
            System.out.println(rtValue);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cosclient.shutdown();
        }
	}
    
    
    
    public static void main(String[] args) {
//    	TencenUploadUtil.init(secretId, secretKey, bucket, region, baseUrl);
        //1.创建文件
//        File file = new File("E:\\home\\csos\\easygoshPath\\3d0487cd4c7c845ae52e8de53450e529.jpeg");
//        String uploadFile = TencenUploadUtil.uploadFile("uploadFilePath/1.jpg",file);
//        System.out.println(uploadFile);
        
        TencenUploadUtil.deleteObject("uploadFilePath/1.jpg");
        
    }


}